package com.sh.demo.qa.module.qa.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sh.demo.qa.module.qa.entity.Question;
import com.sh.demo.qa.module.qa.service.QuestionService;
import com.sh.demo.qa.util.JsonUtil;

@Controller
@RequestMapping(value = "/question")
public class QuestionController {

	protected final static Logger logger = LoggerFactory.getLogger(QuestionController.class);

	@Autowired
	@Qualifier("questionService")
	private QuestionService questionService;

	/**
	 * 数据绑定，使用ServletRequestDataBinder用于对request的参数进行转化
	 */
	@InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }
	
	/**
	 * 查询问题列表，用ResponseBody方式返回json
	 */
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public @ResponseBody String search(
			@RequestParam(value = "currentpage", defaultValue="1") int currentPage, 
			@RequestParam(value = "pagesize", defaultValue="1") int pageSize,
			@RequestParam(value = "sorttype", defaultValue = "auto") String sortType,
			@RequestParam(value = "starttime") Date startTime,
			@RequestParam(value = "endtime") Date endTime,
			Question info
	) throws Exception {
		
		// 查询分页信息
		Page<Question> page = questionService.getListByPage(currentPage, pageSize, sortType, startTime, endTime, info);
		
		// 返回分页信息
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("currentpage", page.getNumber() + 1);
		result.put("totalpage", page.getTotalPages());
		result.put("list", page.getContent());
		String json = JsonUtil.outputJsonByFilter("", "", result, true);
		logger.debug(json);
		return json;
	}

	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public @ResponseBody String get(Question info) throws Exception {
		String result = "中国人";
		return result;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody String add(Question info) throws Exception {
		
		Boolean result = false;
		logger.debug("detail: " + info.getDetail());
		result = questionService.addQuestion(info);
		return result.toString();
	}
	
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public @ResponseBody String modify(Question info) throws Exception {
		
		Boolean result = false;
		return result.toString();
	}
	
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public @ResponseBody String remove(Question info) throws Exception {
		
		Boolean result = false;
		return result.toString();
	}
}
